//#include "SweptAABB.h"
//
//// describes an axis-aligned rectangle with a velocity
//struct Box
//{
//    Box(float _x, float _y, float _w, float _h, float _vx, float _vy)
//    {
//        x = _x;
//        y = _y;
//        w = _w;
//        h = _h;
//        vx = _vx;
//        vy = _vy;
//    }
//
//    Box(float _x, float _y, float _w, float _h)
//    {
//        x = _x;
//        y = _y;
//        w = _w;
//        h = _h;
//        vx = 0.0f;
//        vy = 0.0f;
//    }
//
//	// position of top-left corner
//    float x, y;
//
//	// dimensions
//	float w, h;
//
//	// velocity
//	float vx, vy;
//};
//
//// returns true if the boxes are colliding (velocities are not used)
//bool AABBCheck(Box b1, Box b2)
//{
//    return !(b1.x + b1.w < b2.x || b1.x > b2.x + b2.w || b1.y + b1.h < b2.y || b1.y > b2.y + b2.h);
//}
//
//bool AABBCheck(CObject obj1, CObject obj2)
//{
//	return !(obj1.GetBond().right < obj2.GetBond().left || obj1.GetBond().left > obj2.GetBond().right 
//		|| obj1.GetBond().bottom < obj2.GetBond().top || obj1.GetBond().top > obj2.GetBond().bottom);
//}
//
//bool AABBCheck(RECT rect1, RECT rect2)
//{
//	return !(rect1.right < rect2.left || rect1.left > rect2.right 
//		|| rect1.bottom < rect2.top || rect1.top > rect2.bottom);
//}
//
//// returns true if the boxes are colliding (velocities are not used)
//// moveX and moveY will return the movement the b1 must move to avoid the collision
//bool AABB(Box b1, Box b2, float& moveX, float& moveY)
//{
//    moveX = moveY = 0.0f;
//
//    float l = b2.x - ( b1.x + b1.w );
//    float r = ( b2.x + b2.w ) - b1.x;
//    float t = b2.y - ( b1.y + b1.h );
//    float b = ( b2.y + b2.h ) - b1.y;
//	
//	// check that there was a collision
//    if (l > 0 || r < 0 || t > 0 || b < 0)
//        return false;
//
//	// find the offset of both sides
//    moveX = abs(l) < r ? l : r;
//    moveY = abs(t) < b ? t : b;
//
//	// only use whichever offset is the smallest
//    if (abs(moveX) < abs(moveY))
//        moveY = 0.0f;
//    else
//        moveX = 0.0f;
//
//    return true;
//}
//
//// returns a box the spans both a current box and the destination box
//Box GetSweptBroadphaseBox(Box b)
//{
//    Box broadphasebox(0.0f, 0.0f, 0.0f, 0.0f);
//
//    broadphasebox.x = b.vx > 0 ? b.x : b.x + b.vx;
//    broadphasebox.y = b.vy > 0 ? b.y : b.y + b.vy;
//    broadphasebox.w = b.vx > 0 ? b.vx + b.w : b.w - b.vx;
//    broadphasebox.h = b.vy > 0 ? b.vy + b.h : b.h - b.vy;
//
//    return broadphasebox;
//}
//
//RECT GetSweptBroadphaseBox(CObject obj)
//{
//	RECT broadphasebox;
//	broadphasebox.left = obj.GetVelocity().x > 0 ? obj.GetBond().left : obj.GetBond().left + obj.GetVelocity().x;
//	broadphasebox.top = obj.GetVelocity().y > 0 ? obj.GetBond().top : obj.GetBond().top + obj.GetVelocity().y;
//	broadphasebox.right = obj.GetVelocity().x > 0 ? obj.GetBond().right + obj.GetVelocity().x : obj.GetBond().right;
//	broadphasebox.bottom = obj.GetVelocity().y > 0 ? obj.GetBond().bottom + obj.GetVelocity().y : obj.GetBond().bottom;
//	return broadphasebox;
//}
//// performs collision detection on moving box b1 and static box b2
//// returns the time that the collision occured (where 0 is the start of the movement and 1 is the destination)
//// getting the new position can be retrieved by box.x = box.x + box.vx * collisiontime
//// normalx and normaly return the normal of the collided surface (this can be used to do a response)
//float SweptAABB(Box b1, Box b2, float& normalx, float& normaly)
//{
//    float xInvEntry, yInvEntry;
//    float xInvExit, yInvExit;
//
//    // find the distance between the objects on the near and far sides for both x and y
//    if (b1.vx > 0.0f)
//    {
//        xInvEntry = b2.x - (b1.x + b1.w);
//        xInvExit = (b2.x + b2.w) - b1.x;
//    }
//    else 
//    {
//        xInvEntry = (b2.x + b2.w) - b1.x;
//        xInvExit = b2.x - (b1.x + b1.w);
//    }
//
//    if (b1.vy > 0.0f)
//    {
//        yInvEntry = b2.y - (b1.y + b1.h);
//        yInvExit = (b2.y + b2.h) - b1.y;
//    }
//    else
//    {
//        yInvEntry = (b2.y + b2.h) - b1.y;
//        yInvExit = b2.y - (b1.y + b1.h);
//    }
//	
//    // find time of collision and time of leaving for each axis (if statement is to prevent divide by zero)
//    float xEntry, yEntry;
//    float xExit, yExit;
//
//    if (b1.vx == 0.0f)
//    {
//        xEntry = -std::numeric_limits<float>::infinity();
//        xExit = std::numeric_limits<float>::infinity();
//    }
//    else
//    {
//        xEntry = xInvEntry / b1.vx;
//        xExit = xInvExit / b1.vx;
//    }
//
//    if (b1.vy == 0.0f)
//    {
//        yEntry = -std::numeric_limits<float>::infinity();
//        yExit = std::numeric_limits<float>::infinity();
//    }
//    else
//    {
//        yEntry = yInvEntry / b1.vy;
//        yExit = yInvExit / b1.vy;
//    }
//	
//    // find the earliest/latest times of collision
//    float entryTime = max(xEntry, yEntry);
//    float exitTime = min(xExit, yExit);
//	
//    // if there was no collision
//    if (entryTime > exitTime || xEntry < 0.0f && yEntry < 0.0f || xEntry > 1.0f || yEntry > 1.0f)
//    {
//        normalx = 0.0f;
//        normaly = 0.0f;
//        return 1.0f;
//    }
//    else // if there was a collision
//    {        		
//        // calculate normal of collided surface
//        if (xEntry > yEntry)
//        {
//            if (xInvEntry < 0.0f)
//            {
//                normalx = 1.0f;
//                normaly = 0.0f;
//            }
//	        else
//            {
//                normalx = -1.0f;
//                normaly = 0.0f;
//            }
//        }
//        else
//        {
//            if (yInvEntry < 0.0f)
//            {
//                normalx = 0.0f;
//                normaly = 1.0f;
//            }
//	        else
//            {
//                normalx = 0.0f;
//		        normaly = -1.0f;
//            }
//        }
//
//        // return the time of collision
//        return entryTime;
//    }
//}
//
//float SweptAABB(CObject obj1, CObject obj2)
//{
//	float xInvEntry, yInvEntry;
//	float xInvExit, yInvExit;
//
//	// find the distance between the objects on the near and far sides for both x and y
//	if (obj1.GetVelocity().x > 0.0f)
//	{
//		xInvEntry = obj2.GetBond().left - obj1.GetBond().right;
//		xInvExit = obj2.GetBond().right - obj1.GetBond().left;
//	}
//	else 
//	{
//		xInvEntry = obj2.GetBond().right - obj1.GetBond().left;
//		xInvExit = obj2.GetBond().left - obj1.GetBond().right;
//	}
//
//	if (obj1.GetVelocity().y > 0.0f)
//	{
//		yInvEntry = obj2.GetBond().top - obj1.GetBond().bottom;
//		yInvExit = obj2.GetBond().bottom - obj1.GetBond().top;
//	}
//	else
//	{
//		yInvEntry = obj2.GetBond().bottom - obj1.GetBond().top;
//		yInvExit = obj2.GetBond().top - obj1.GetBond().bottom;
//	}
//
//	// find time of collision and time of leaving for each axis (if statement is to prevent divide by zero)
//	float xEntry, yEntry;
//	float xExit, yExit;
//
//	if (obj1.GetVelocity().x == 0.0f)
//	{
//		xEntry = -std::numeric_limits<float>::infinity();
//		xExit = std::numeric_limits<float>::infinity();
//	}
//	else
//	{
//		xEntry = xInvEntry / obj1.GetVelocity().x;
//		xExit = xInvExit / obj1.GetVelocity().x;
//	}
//
//	if (obj1.GetVelocity().y == 0.0f)
//	{
//		yEntry = -std::numeric_limits<float>::infinity();
//		yExit = std::numeric_limits<float>::infinity();
//	}
//	else
//	{
//		yEntry = yInvEntry / obj1.GetVelocity().y;
//		yExit = yInvExit / obj1.GetVelocity().y;
//	}
//
//	// find the earliest/latest times of collision
//	float entryTime = max(xEntry, yEntry);
//	float exitTime = min(xExit, yExit);
//
//	// if there was no collision
//	if (entryTime > exitTime || xEntry < 0.0f && yEntry < 0.0f || xEntry > 1.0f || yEntry > 1.0f)
//	{
//		return 1.0f;
//	}
//	else // if there was a collision
//	{        	
//		// return the time of collision
//		return entryTime;
//	}
//}
//
//void MoveAABB(CObject obj1, CObject obj2)
//{
//	RECT broadphasebox = GetSweptBroadphaseBox(obj1);
//	if (AABBCheck(broadphasebox, obj2.GetBond()))
//	{
//		float collisiontime = SweptAABB(obj1, obj2);
//		float remainingtime = 1.0f - collisiontime;
//		//box.x += box.vx * collisiontime;
//		//box.y += box.vy * collisiontime;
//		obj1.UpdatePosition(collisiontime);
//		if (collisiontime < 1.0f)
//		{
//			// perform response here
//			if (obj1.GetBond().top >= obj2.GetBond().bottom)		//obj1 di tu duoi obj2 len //nhay len
//			{
//				obj1.SetVelocityY(-1 * obj1.GetVelocity().y);
//				obj1.UpdatePosition(remainingtime);
//			} 
//			else if (obj1.GetBond().bottom <= obj2.GetBond().top)	//obj1 di tu tren obj2 xun // roi xuong
//			{
//				float temp = obj1.GetVelocity().y;
//				obj1.SetVelocityY(0);
//				obj1.UpdatePosition(remainingtime);
//				obj1.SetVelocityY(temp);
//			} 
//			else
//			{
//				return;
//			}
//		}
//	}
//}